package com.hl.controller;

import com.hl.domain.Publicity;
import com.hl.service.PublicityService;
import com.hl.util.R;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;

@RestController
@RequestMapping("/publicity")
public class PublicityController {

    @Autowired
    private PublicityService publicityController;

    @Autowired
    private PublicityService publicityService;
//    查询所有通知公告内容
    @GetMapping("/selectAll")
    public R selectAll(int pageNum,int pageSize){
        return R.success(publicityService.selectAll(pageNum,pageSize));
    }

    //   添加数据
    @PostMapping("/addNotice")
    public R addUser(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Publicity publicity) {
        System.out.println(publicity.getCreatetime());
        return R.success(publicityController.addNotice(publicity));
    }

    //    删除数据
    @DeleteMapping("/deleteNotice")
        public R deleteUser(long id) {
        return R.success(publicityController.deleteNotice(id));
    }
//    修改数据
    @PutMapping("/updateNotice")
    public R updateUser(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Publicity publicity) {
        return R.success(publicityController.updateNotice(publicity));
    }
//   数据展示
    @GetMapping("/fetchAll")
    public R UserAll(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") int page, int size) {
        return R.success(publicityController.fetchAll(page, size));
    }

    //    条件查询数据
    @GetMapping("/searchNotice")
    public R TermsAll( @RequestParam(required = false) String name,
                       @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime begintime,
                       @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endtime,
                       @RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "10") int size) {
        return R.success(publicityController.search(name, begintime, endtime, page, size));
    }
}
